package cn.hznc.controller;

import cn.hznc.controller.service.QuerySqlServerService;
import cn.hznc.controller.service.inventory.ToolPartsInventoryService;
import cn.hznc.controller.service.order.AbutmentService;
import cn.hznc.controller.service.schedule.ToolScheduleService;
import cn.hznc.controller.service.toolAnalyze.*;
import cn.hznc.domain.entity.*;
import cn.hznc.domain.request.inventory.QueryAllToolPartReq;
import cn.hznc.domain.request.toolAnalyze.PersonnelUsageAnalysisReq;
import cn.hznc.domain.response.PageResult;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.excel.ToolImportAnalysisListener;
import cn.hznc.exception.SystemException;
import cn.hznc.utils.FileUploadUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/test")
@Slf4j
public class QuerySqlServerTestController {
    @Resource
    private QuerySqlServerService querySqlServerService;
    @Resource
    private AbutmentService abutmentService;
    @Resource
    private ToolConsumptionandUsageAnalysisService toolConsumptionandUsageAnalysisService;
    @Resource
    private ToolDemandForecastReportService toolDemandForecastReportService;
    @Resource
    private PersonnelUsageAnalysisService personnelUsageAnalysisService;
    @Resource
    private ToolScheduleService toolScheduleService;
    @Resource
    private ToolPartsInventoryService toolPartsInventoryService;
    @Resource
    private ToolImportAnalysisService toolImportAnalysisService;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ResponseBody test1(@RequestParam Integer id){
        return ResponseBody.success(toolConsumptionandUsageAnalysisService.deleteTypeById(id));
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public ResponseBody<List<TypeEntity>> test2(){
        return ResponseBody.success(
                toolConsumptionandUsageAnalysisService.queryAllType()
        );
    }
//
    @RequestMapping(value = "/test3", method = RequestMethod.GET)
    public ResponseBody test3(@RequestParam(defaultValue = "0") Integer parentId,
                              @RequestParam String typeName){
//        return ResponseBody.success();
        try{
            toolConsumptionandUsageAnalysisService.insertType(parentId,typeName);
            return ResponseBody.success();
        }catch (Exception ex){
            return ResponseBody.error(ex.getMessage(),ex.getMessage());
        }
    }
    @RequestMapping(value = "/test4", method = RequestMethod.GET)
    public ResponseBody test4(@RequestParam Integer id,
                              @RequestParam String typeName){
        try{
            toolConsumptionandUsageAnalysisService.updateType(id,typeName);
            return ResponseBody.success();
        }catch (Exception ex){
            return ResponseBody.error(ex.getMessage(),ex.getMessage());
        }
    }
    @RequestMapping(value = "/test5", method = RequestMethod.POST)
    public ResponseBody<PageResult<PersonnelUsageAnalysisEntity>> test5(@RequestBody PersonnelUsageAnalysisReq personnelUsageAnalysisReq){
        return ResponseBody.success(personnelUsageAnalysisService.queryPersonnelUsageConsume(personnelUsageAnalysisReq));
    }

    @RequestMapping(value = "/test6", method = RequestMethod.POST)
    public ResponseBody<PageResult<PersonnelUsageAnalysisEntity>> test6(@RequestBody PersonnelUsageAnalysisReq personnelUsageAnalysisReq){
        return ResponseBody.success(personnelUsageAnalysisService.queryKnifeInstallation(personnelUsageAnalysisReq));
    }
    @RequestMapping(value = "/test7", method = RequestMethod.GET)
    public ResponseBody<List<NameNumberEntity>> test7(){
        return ResponseBody.success(toolScheduleService.queryMaximumMaterialUsage());
    }
    @RequestMapping(value = "/test8", method = RequestMethod.GET)
    public ResponseBody<List<NameNumberEntity>> test8(){
        return ResponseBody.success(toolScheduleService.queryReclaimingCategory());
    }

    @RequestMapping(value = "/test9", method = RequestMethod.POST)
    public ResponseBody<PageResult<ToolPartEntity>> test9(@RequestBody QueryAllToolPartReq queryAllToolPartReq){
        return ResponseBody.success(toolPartsInventoryService.queryAllToolPart(queryAllToolPartReq));
    }

//    @RequestMapping(value = "/test10", method = RequestMethod.GET)
//    public ResponseBody queryAssemblyRetrieval(@RequestParam String taskId,
//                                               @RequestParam Integer pageNo,
//                                               @RequestParam Integer pageSize) throws ToolScheduleException {
//        return ResponseBody.success(toolDemandForecastReportService.queryToolDemandForecastReport(taskId,pageNo,pageSize));
//    }

    @RequestMapping(value = "/test11", method = RequestMethod.POST)
    public ResponseBody<Boolean> importAllotEmployee(@RequestPart("file") MultipartFile file,
                                                     @RequestParam(value = "warehouseId",required = false) String warehouseId,
                                                     @RequestParam(value = "operator",required = false) String operator) {
        List<ToolImportAnalysisEntity> toolImportAnalysisEntities = new ArrayList<>();
        try {
            ToolImportAnalysisListener toolImportAnalysisListener = new ToolImportAnalysisListener(toolImportAnalysisEntities);
            FileUploadUtil.checkCommonFile(file);
            EasyExcel.read(file.getInputStream(),ToolImportAnalysisEntity.class, toolImportAnalysisListener).extraRead(CellExtraTypeEnum.MERGE).headRowNumber(2).autoCloseStream(true).sheet().doRead();
            toolImportAnalysisService.importToolAnalysis(toolImportAnalysisListener.getImportData(),Integer.valueOf(warehouseId),operator);
        } catch (IOException ex) {
            log.error("importOrder error========》", ex);
        } catch (SystemException systemException) {
            log.error("importOrder error========》msg：{}", systemException.getMsg());
            return ResponseBody.error(systemException.getCode(), systemException.getMsg());
        } catch (IllegalAccessException e) {
            log.error("importOrder error========》" , e.getMessage());
            return ResponseBody.error("1001" , "解析错误1");
        } catch (ParseException e) {
            log.error("importOrder error========》" , e.getMessage());
            return ResponseBody.error("1002" , "解析错误2");
        }
        return ResponseBody.success(Boolean.TRUE);
    }
}
